package com.zf.yichat.utils.HJpay;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zf.yichat.utils.HJpay.com.sdk.entity.RequestParam;
import com.zf.yichat.utils.HJpay.com.sdk.enums.SignTypeEnum;
import com.zf.yichat.utils.HJpay.com.sdk.utils.AESUtil;
import com.zf.yichat.utils.HJpay.com.sdk.utils.JsonUtil;
import com.zf.yichat.utils.HJpay.com.sdk.utils.RSAUtil;
import com.zf.yichat.utils.HJpay.com.sdk.utils.SignUtil;
import com.zf.yichat.utils.HJpay.util.HttpClientUtil;

/**
 * 快捷支付-协议支付接口
 *
 * @author Lori
 * @version 2019年11月
 */
public class HjPayUtil {
    public static final String METHOD_AGREEMENT_PAY_SMS = "fastPay.agreement.paySms";
    public static final String METHOD_AGREEMENT_PAY = "fastPay.agreement.smsPay";
    public static final String METHOD_AGREEMENT_SIGN_SMS = "fastPay.agreement.signSms";
    public static final String METHOD_AGREEMENT_SIGN = "fastPay.agreement.smsSign";

    public static String getHjResponse(String secretKey, String aesKey, String publicKey, String mchNo, String randStr, Map<String, Object> map, String method) throws Exception {

        RequestParam requestParam = new RequestParam();
        requestParam.setMch_no(mchNo);// 商户编号
        requestParam.setMethod(method);// 固定方法名
        requestParam.setVersion("1.0");// 版本号
        requestParam.setRand_str(randStr);// 随机字符串
        requestParam.setSign_type("2");// 签名类型
        requestParam.setSec_key(RSAUtil.encryptByPublicKey(publicKey, aesKey));//加密密钥
        requestParam.setData(JsonUtil.toString(map));

        String signStr = getSortedString(requestParam);
        System.out.println("待签名字符串：" + signStr);// 待签名字符串

        // 签名
        if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
            signStr = (RSAUtil.sign(signStr, secretKey, false));
        } else {
            signStr = "";
            System.out.println("未预期的签名类型：" + requestParam.getSign_type());
        }
        requestParam.setSign(signStr);


        // Map转json字符串
        String reqBodyJson = JSON.toJSONString(requestParam);
        System.out.println(reqBodyJson);
        String httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
        System.out.println(httpResponseJson);
        return httpResponseJson;
    }


    public static String getSortedString(Object obj) throws Exception {
        Field[] fields = obj.getClass().getDeclaredFields();

        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < fields.length; i++) {
            Field filed = fields[i];
            String name = filed.getName();
            if (SignUtil.NOT_SIGN_PARAM.contains(name)) {// 不参与签名或验签的参数直接跳过
                continue;
            }
            filed.setAccessible(true);
            map.put(name, String.valueOf(filed.get(obj)));
        }
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList(map.keySet());
        Collections.sort(keys); // 排序map
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = map.get(key);

            if (i != 0) {
                content.append(SignUtil.SIGN_SEPARATOR);
            }
            content.append(key).append(SignUtil.SIGN_EQUAL).append(value);
        }
        return content.toString();

    }
}
